<?php

class Story_Widget_CreateNovelStoryController extends
    Engine_Content_Widget_Abstract
{

    public function indexAction()
    {    	
    	$viewer = Engine_Api::_()->user()->getViewer();
    	$this->view->can_create = Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'create');
    	
    	if (!$this->view->can_create) {
    		return $this->setNoRender();
    	}
    	
    	$this->view->form = $form = new Story_Form_Create_Custom(array(
    			'category' => Story_Model_Category::NOVEL,
    			'tit' => 'Novel',
    	));
    	
    	$story = null;
    	$chapter_number = 1;
    	$prevChapter = null;
    	if( Engine_Api::_()->core()->hasSubject() ) {
    		$story = Engine_Api::_()->core()->getSubject();
    		
    		if(!$story->canView(Story_Model_Category::NOVEL)){
    			return $this->setNoRender();
    		}
    		
    		$request = Zend_Controller_Front::getInstance()->getRequest();
    		$chapter_id = $request->getParam('chapter_id');
    		$prevChapter = $story->getLastChapterThread();
    		
    		
    		
    		$form->title->setValue($story->getTitle());
    		$body = "body_" . $story->category_id;
    		if($form->$body){
    			$form->$body->setValue(strip_tags($story->body));
    		}
    		$form->story_id->setValue($story->getIdentity());    		
    		$chapter_number = $story->getTotalChapters();
    		$this->view->story_id = $story->getIdentity();
            $this->view->lastChapter = $story->getLastChapter();
    	}
    	
    	$this->view->story = $story;
    	$this->view->chapter_number = $chapter_number;
    	$this->view->prevChapter = $prevChapter;

    }

}
